// 格式化时间
export function changeDate(dateA) {
  let dateee = new Date(dateA).toJSON()
  let date = new Date(+new Date(dateee) + 8 * 3600 * 1000)
    .toISOString()
    .replace(/\.[\d]{3}Z/, '')
  return date
}

// 获取现在的时间,包括时分秒
export function formatNewDate(str = 'yyyy-MM-dd hh:mm:ss') {
  let newDateString = ''
  let date = new Date()
  let Year = date.getFullYear()
  let Month = date.getMonth() + 1
  let Day = date.getDate()
  let Hours = date.getHours()
  let Minutes = date.getMinutes()
  let Seconds = date.getSeconds()

  Month = Month < 10 ? '0' + Month : Month
  Day = Day < 10 ? '0' + Day : Day
  Hours = Hours < 10 ? '0' + Hours : Hours
  Minutes = Minutes < 10 ? '0' + Minutes : Minutes
  Seconds = Seconds < 10 ? '0' + Seconds : Seconds
  newDateString = `${Year}-${Month}-${Day} ${Hours}:${Minutes}:${Seconds}`
  return newDateString
}

// 获取当前月份
// isTime 这个参数设置时分秒,默认获取
export function getCurrentMonthFirst(isTime = true) {
  let startTime = ''
  let date = new Date()
  date.setDate(1)
  let month = parseInt(date.getMonth() + 1)
  let day = date.getDate()
  let hours = date.getHours()
  let minutes = date.getMinutes()
  let seconds = date.getSeconds()
  if (month < 10) month = '0' + month
  if (day < 10) day = '0' + day

  startTime = `${date.getFullYear()}-${month}-${day}`
  startTime = isTime ? `${startTime} 00:00:00` : startTime
  return startTime
}

// 获取当前时间
// isTime 这个参数设置时分秒,默认获取
export function getCurrentMonthLast(isTime = true) {
  let endTime = ''
  let date = new Date()
  let Year = date.getFullYear()
  let month = parseInt(date.getMonth() + 1)
  let day = date.getDate()
  let hours = date.getHours()
  let minutes = date.getMinutes()
  let seconds = date.getSeconds()
  if (month < 10) month = '0' + month
  if (day < 10) day = '0' + day

  endTime = `${Year}-${month}-${day}`
  endTime = isTime ? `${endTime} 23:59:59` : endTime
  return endTime
}

// 获取年月份
export function getMonth() {
  let time = ''
  let date = new Date()
  let Year = date.getFullYear()
  let month = parseInt(date.getMonth() + 1)
  if (month < 10) month = '0' + month
  time = `${Year}-${month}`
  return time
}

// 深克隆
export function deepClone(data) {
  if (!data) return
  return JSON.parse(JSON.stringify(data))
}

// 浅
export function turnToString(data) {
  if (!data) return
  return JSON.stringify(data)
}

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time)
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key] // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

/**
 *
 * @param {*} year 年
 * @param {*} month 月
 */
export function endOfMonth(year, month) {
  return new Date(year, month, 0).getDate() // 获取本月最后一天
}

/**
 *
 * @param {*} fmt 时间格式
 * @returns
 */
export function formatDate(fmt = 'yyyy-MM-dd hh:mm:ss') {
  const date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let seaond = date.getSeconds()
  day = day <= 9 ? `0${day}` : day
  hour = hour <= 9 ? `0${hour}` : hour
  minute = minute <= 9 ? `0${minute}` : minute
  seaond = seaond <= 9 ? `0${seaond}` : seaond
  let args = {
    'y+': year,
    'M+': month,
    'd+': day,
    'h+': hour,
    'm+': minute,
    's+': seaond
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (year + '').substring(4 - RegExp.$1.length))
  }

  for (let k in args) {
    if (new RegExp(`(${k})`).test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1
          ? args[k]
          : ('00' + args[k]).substring(('' + args[k]).length)
      )
    }
  }
  return fmt
}

// 生成随机字符
export function randomCharacters() {
  const time = new Date().getTime().toString()
  const timeArr = time.split('')
  const arr = 'abcdefghijklmnopqrstuvwxyzABCDEFG'.split('')
  timeArr.forEach((item, index) => {
    const random = arr[Math.floor(Math.random() * arr.length)]
    timeArr.splice(index * 2, 0, random)
  })
  return timeArr.join('')
}

/**
 * 数组去重
 * @param {*} arr 接收的原数组
 * @param {*} key 如果是对象数组[{id: 1}, {id: 2}, {id: 3}]，则需要以什么key作为重复的标准，普通数组[1,2,3,2]不需要
 */
export function arrUnique(arr, key) {
  let returnArr = []
  if (key) {
    // 对象数组去重
    const obj = {}
    returnArr = arr.reduce((cur, next) => {
      obj[next[key]] ? '' : (obj[next[key]] = true && cur.push(next))
      return cur
    }, [])
    return returnArr
  }
  // 普通数组去重
  returnArr = arr.reduce((cur, next) => {
    !cur.includes(next) && cur.push(next)
    return cur
  }, [])
  return returnArr
}

